﻿#include "light.h"
#include <QDebug>


Light::Light(QObject *parent) :
    QObject(parent)
{
    serial_port_ = new QSerialPort(this);
}

Light::~Light(){

    if(serial_port_ != nullptr){
        serial_port_->close();
    }

    delete serial_port_;

}


   bool Light::SerialPortInit(){

       //

       return false;
   }


   bool Light::setSerialParam(SerialParam param){

       if(param.port == "COM2" || param.port == "COM4"){
           serial_port_->setPortName(param.port);
       }

       if(param.baud == "9600"){
           serial_port_->setBaudRate(QSerialPort::Baud9600);
       }

       // 校验，校验默认选择无
       if(param.parity == "None"){
           serial_port_->setParity(QSerialPort::NoParity);
       }

       // 数据位，数据位默认选择8位
       if(param.dataBit == "8"){
           serial_port_->setDataBits(QSerialPort::Data8);
       }

       // 停止位，停止位默认选择1位
       if(param.stopBits == "1"){
           serial_port_->setStopBits(QSerialPort::OneStop);
       }

       // 控制流，默认选择无
       serial_port_->setFlowControl(QSerialPort::NoFlowControl);


        if(serial_port_->open(QIODevice::ReadWrite)){
            qDebug() << " open serial succuss!";
            return true;
        }
        else{
            return false;
        }
   }



   bool Light::openLight1(){

       QByteArray openTheLight;
       openTheLight.append('\x24');
       openTheLight.append('\x01');
       openTheLight.append('\x1c');
       openTheLight.append('\x24'^'\x01'^'\x1c');
       serial_port_->write(openTheLight);
       serial_port_->waitForBytesWritten(5000);
       return true;
   }


   bool Light::closeLight1(){

       QByteArray closeTheLight;
       closeTheLight.append('\x24');
       closeTheLight.append('\x01');
       closeTheLight.append('\x00');
       closeTheLight.append('\x24'^'\x01'^'\x00');
       serial_port_->write(closeTheLight);
       serial_port_->waitForBytesWritten(5000);
       return true;
   }

   void Light::setLuminance1(int lightLuminance)
   {
       QByteArray setLuminance;
       setLuminance.append('\x24');
       setLuminance.append('\x01');
       setLuminance.append(lightLuminance);
       setLuminance.append('\x24'^'\x01'^lightLuminance);
       serial_port_->write(setLuminance);
       bool  state = serial_port_->waitForBytesWritten(1000);

       if(state == false){
           serial_port_->write(setLuminance);
           state = serial_port_->waitForBytesWritten(1000);
       }

       if(state == false){
           serial_port_->write(setLuminance);
           state = serial_port_->waitForBytesWritten(1000);
       }
   }

   bool Light::openLight2(){

       QByteArray openTheLight;
       openTheLight.append('\x24');
       openTheLight.append('\x02');
       openTheLight.append('\x1c');
       openTheLight.append('\x24'^'\x02'^'\x1c');
       serial_port_->write(openTheLight);
       serial_port_->waitForBytesWritten(5000);
       return true;
   }


   bool Light::closeLight2(){

       QByteArray closeTheLight;
       closeTheLight.append('\x24');
       closeTheLight.append('\x02');
       closeTheLight.append('\x00');
       closeTheLight.append('\x24'^'\x02'^'\x00');
       serial_port_->write(closeTheLight);
       serial_port_->waitForBytesWritten(5000);
       return true;
   }

   void Light::setLuminance2(int lightLuminance)
   {
       QByteArray setLuminance;
       setLuminance.append('\x24');
       setLuminance.append('\x02');
       setLuminance.append(lightLuminance);
       setLuminance.append('\x24'^'\x02'^lightLuminance);
       serial_port_->write(setLuminance);
       bool state = serial_port_->waitForBytesWritten(1000);

       if(state == false){
           serial_port_->write(setLuminance);
           state = serial_port_->waitForBytesWritten(1000);
       }

       if(state == false){
           serial_port_->write(setLuminance);
           state = serial_port_->waitForBytesWritten(1000);
       }

   }
